<template>
  <div class="serviceList">
    <!-- 搜索 -->
    <div class="section">
      <h3>开服列表</h3>
      <search-item
        @searchList="searchData"
        @showaddserviceDialog="showaddserviceDialog"
        @showExcel="showExcel"
      ></search-item>
    </div>
    <!-- 表格 -->
    <el-table
      :height="tabHeight"
      border
      style="width: 99%"
      align="center"
      :data="tableData"
      @selection-change="selectTable"
      v-loading="loading"
    >
      <el-table-column type="selection" width="35"></el-table-column>
      <el-table-column prop="id" label="序号ID" width="100"></el-table-column>
      <el-table-column label="游戏名">
        <template slot-scope="scope">
          <img
            :src="scope.row.icon"
            alt
            v-if="scope.row.icon!=''"
            style="width: 44px;height: 44px;margin:0 auto;display:block;"
          />
          <span class="gamename">{{scope.row.name}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="ser_name" label="开服名称"></el-table-column>
      <el-table-column prop="start_time" label="开服时间"></el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status===0" type="text" size="small" style="color:#D7DAE2">编辑</el-button>
          <el-button v-else type="text" size="small" @click="showeditDialog(scope.row)">编辑</el-button>
          <el-button
            type="text"
            size="small"
            @click="deleteDialog(scope.row)"
            style="color:#FB4665"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="bottom">
      <!-- 将分页器作为子组件引入 -->
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page.sync="pagenationData.currentPage"
        :page-size="pagenationData.pageSize"
        :total="pagenationData.pageTotal"
        background
        layout="prev, pager, next"
        prev-text="上一页"
        next-text="下一页"
      ></el-pagination>
      <el-button type="primary" @click="selectDelete" class="selectDelete">批量删除</el-button>
    </div>
    <!-- 添加游戏 -->
    <AddServiceDialog
      v-if="addservicedialog.show"
      :isShow="addservicedialog.show"
      :dialogRow="addservicedialog.dialogRow"
      @closeDialog="hideaddserviceDialog"
    ></AddServiceDialog>
    <EditServiceDialog
      v-if="editservicedialog.show"
      :isShow="editservicedialog.show"
      :dialogRow="editservicedialog.dialogRow"
      @closeDialog="hideeditserviceDialog"
      :cpId="cpId"
    ></EditServiceDialog>
  </div>
</template>
<script>
import SearchItem from "./component/searchItem";
import AddServiceDialog from "./component/addservice";
import EditServiceDialog from "./component/editService";

import {
  getserviceList,
  editServicelist,
  exportExcel,
  delGameserver,
  delsGameserver,
} from "@/api/openService";
export default {
  name: "serviceList",
  inject: ["reload"],
  data() {
    return {
      tabHeight: window.innerHeight - 200,
      tableData: [],
      multipleSelection: [], //复选框选中
      search: {},
      loading: true,
      pagenationData: {
        pageTotal: 0,
        currentPage: 1,
        pageSize: 20,
      },
      pageData: {
        page: 1,
        limit: 20,
      },
      addservicedialog: {
        show: false,
        dialogRow: {},
      },
      editservicedialog: {
        show: false,
        dialogRow: {},
      },
      cpId: "",
    };
  },
  components: {
    SearchItem,
    AddServiceDialog,
    EditServiceDialog,
  },
  mounted() {
    this.getserviceList();
  },
  methods: {
    searchData(search) {
      this.search = search;
      this.search.page = 1;
      this.pagenationData.currentPage = 1;
      this.searchList();
    },
    // 搜索数据
    searchList() {
      this.loading = true;
      const para = Object.assign({}, this.search);
      getserviceList(para)
        .then((res) => {
          this.loading = false;
          this.pagenationData.pageTotal = res.data.count;
          this.tableData = res.data.list;
        })
        .catch((error) => {});
    },
    // 获取数据列表
    getserviceList() {
      const para = Object.assign({}, this.pageData);
      getserviceList(para)
        .then((res) => {
          this.cpId = res.data.cpId;
          this.loading = false;
          this.pagenationData.pageTotal = res.data.count;
          this.tableData = res.data.list;
        })
        .catch((error) => {});
    },
    handleCurrentChange(val) {
      this.pageData.page = val;
      this.search.page = val;
      this.searchList();
    },
    // 显示添加开服信息
    showaddserviceDialog() {
      this.addservicedialog.show = true;
    },
    hideaddserviceDialog() {
      this.addservicedialog.show = false;
    },
    // 编辑
    showeditDialog(row) {
      const obj = {
        serverId: row.id,
      };
      const para = Object.assign({}, obj);
      editServicelist(para)
        .then((res) => {
          this.editservicedialog.dialogRow = res.data;
          this.editservicedialog.show = true;
        })
        .catch((error) => {});
    },
    // 删除
    deleteDialog(row) {
      this.$confirm("确认删除此条数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          const para = Object.assign({}, { serverId: row.id });
          delGameserver(para).then((res) => {
            this.$message.success(res.msg);
            this.reload();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    hideeditserviceDialog() {
      this.editservicedialog.show = false;
    },
    showExcel(data) {
      setTimeout(() => {
        this.reload();
      }, 20);
    },
    // 当用户手动勾选数据行的 Checkbox 时触发的事件
    selectTable(val, row) {
      this.multipleSelection = val;
    },
    selectDelete() {
      let len = this.multipleSelection.length;
      this.$confirm(
        `批量删除后无法找回，您确认要删除这${len}条内容吗?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true,
        }
      )
        .then(() => {
          var idArr = [];
          this.multipleSelection.forEach((item) => {
            idArr.push(item.id);
          });
          var para = Object.assign({}, { serverIds: idArr });
          delsGameserver(para).then((res) => {
            this.$message.success(res.msg);
            this.searchList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>
<style lang="scss">
.serviceList {
  .selectDelete {
    padding: 7px 20px;
    font-size: 13px;
    margin-bottom: 5px;
    margin-left: 20px;
  }
  .upload-demo .el-upload-list {
    display: none !important;
  }
  .section {
    justify-content: flex-start;
  }

  .gamename {
    width: 150px;
    display: inline-block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
.el-message-box__content {
  font-size: 13px;
}
.el-table th div {
  line-height: 23px;
}
.el-message-box__title,
.el-message-box__btns {
  text-align: center;
}
.el-message-box .el-button--small {
  padding: 9px 30px;
}
.el-message-box {
  padding: 20px 0;
}
.el-message-box__content {
  padding: 25px;
}
.el-table__row {
  .el-table_1_column_2 {
    text-align: left;
  }
}
</style>